package com.example.administrator.social.adapter;

import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.administrator.social.R;
import com.example.administrator.social.view.CircleImageView;

import java.util.List;

/**
 * Created by Administrator on 2018/9/11 0011.
 */
public abstract class CharBaseAdapter<T> extends RecyclerView.Adapter<CharBaseAdapter.ViewHolder> {
    public Context mContext;
    public LayoutInflater mInflater;
    public List<T> mData;
    public static final int TYPE_RECEIVED = 0;
    public static final int TYPE_SEND = 1;

    public CharBaseAdapter(Context context, List<T> data){
        mData = data;
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载布局文件
        View view = null;
        switch (viewType){
            case 1:
                view = mInflater.inflate(R.layout.char_text_item,parent,false);
                break;
            case 2:
                view = mInflater.inflate(R.layout.char_voice_item,parent,false);
                break;
            case 3:
                view = mInflater.inflate(R.layout.char_image_item,parent,false);
                break;
            case 4:
                view = mInflater.inflate(R.layout.char_video_item,parent,false);
                break;
        }
        view.setTag(viewType);
        return new ViewHolder(view);
    }


    @Override
    public int getItemViewType(int position) {
        return getContentType(position);
    }

    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        } else {
            return 0;
        }
    }

   // public abstract void initViewHolder(View holder);
    public abstract int getContentType(int position);

    public class ViewHolder extends RecyclerView.ViewHolder {
        public CircleImageView mCharLeftHeaderImage,mCharRightHeaderImage;
        //text message
        public LinearLayout mCharTextLeftLayout,mCharTextRightLayout;
        public TextView mCharLeftText,mCharRightText;
        //image message
        public ImageView mMessageSendErrorRight;
        public ProgressBar mCharTextProgressBarRight;

        public ViewHolder(View itemView) {
            super(itemView);
            switch ((int)itemView.getTag()){
                case 1:
                    initCharText(itemView);break;
                case 2:
                    initCharImage(itemView);break;
                case 3:
                    initViewVoice(itemView);break;
                case 4:
                    initViewVideo(itemView);break;
            }
        }


        protected  void initCharText(View itemView){
            //right
            mCharRightHeaderImage = itemView.findViewById(R.id.char_header_right_image);
            mCharTextRightLayout = itemView.findViewById(R.id.char_text_right_layout);
            mCharRightText = itemView.findViewById(R.id.char_right_text);
            mMessageSendErrorRight = itemView.findViewById(R.id.char_text_message_send_error_right);
            mCharTextProgressBarRight = itemView.findViewById(R.id.progressBar_char_text_right);
            //left
            mCharLeftText = itemView.findViewById(R.id.char_left_text);
            mCharTextLeftLayout = itemView.findViewById(R.id.char_text_left_layout);
            mCharLeftHeaderImage = itemView.findViewById(R.id.char_header_left_image);

        }
        protected  void initCharImage(View itemView){}
        protected  void initViewVoice(View itemView){}
        protected  void initViewVideo(View itemView){}

    }

    public void updateDataItem(T obj){
        mData.add(obj);
        notifyItemInserted(mData.size()-1);
    }

    public void updateListDate(List<T> data){
        mData = data;
        notifyDataSetChanged();
    }
}
